package com.jinsi.controller.zhu;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinsi.pojo.HomeLoginLogs;
 import com.jinsi.service.zhu.HomeLoginService;
import com.jinsi.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * （失败或者成功的）登陆日志
 * @author 朱浩榕
 * @Date 2022-10-13
 */
@RestController
@RequestMapping("/land")
@Slf4j
public class HomeLoginController {
    @Autowired
    private HomeLoginService homeLoginService;

    /**
     * 查询登陆日志
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/getAll")
    public R getAllandPage(Integer page, Integer limit){
        Map<String,Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);
        List<HomeLoginLogs> all = homeLoginService.getAllandPage();
        PageInfo<HomeLoginLogs> pageInfo = new PageInfo<>(all);
        map.put("total",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        map.put("state",0);
        map.put("msg","成功");
        return R.ok(map);
    }


    /**
     * 登陆失败添加数据
     * @param errorusername 错误的用户名
     * @param errorpassword 错误的密码
     * @param zhuangtai 登陆者的登陆状态
     * @param zip 登陆者的IP
     * @param date 登陆时间
     * @return
     */
    @RequestMapping("/erroraddlog")
    public Map<String,Object> insert(String errorusername,String errorpassword,String zhuangtai,String zip,Date date){
        zip = null;
        Date date1 = new Date();
        Map<String,Object> map = new HashMap<>();
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
            zip = addr.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        map.put("username",errorusername);
        map.put("password",errorpassword);
        map.put("zhuangtai",zhuangtai);
        map.put("ip",zip);
        map.put("date",date1);
        homeLoginService.erroradd(errorusername,errorpassword,zhuangtai,zip,date1);

        return map;
    }

    /**
     * 登陆成功添加数据
     * @param errorusername 错误的用户名
     * @param errorpassword 错误的密码
     * @param zhuangtai 登陆者的登陆状态
     * @param zip 登陆者的IP
     * @param date 登陆时间
     * @return
     */
    @RequestMapping("/successaddlog")
    public Map<String,Object> insertB(String errorusername,String errorpassword,String zhuangtai,String zip,Date date){
        zip = null;
        Date date1 = new Date();
        Map<String,Object> map = new HashMap<>();
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
            zip = addr.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        map.put("username",errorusername);
        map.put("password",errorpassword);
        map.put("zhuangtai",zhuangtai);
        map.put("ip",zip);
        map.put("date",date1);
        homeLoginService.successadd(errorusername,errorpassword,zhuangtai,zip,date1);
        return map ;
    }

}
